// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.chimi.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.chimi.controller.request.QueryBarLuckReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarLuckReq;
import com.liuxinlong.modules.chimi.service.BarLuckService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.BarLuck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 论坛活动管理控制层
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/luck")
public class BarLuckController {

	@Autowired
	private BarLuckService barLuckService;

	/**
	 * 分页查询活动配置信息
	 * 
	 * @param req 请求信息
	 * @return 活动配置信息列表
	 */
	@PostMapping("page")
	public BaseResp pageLuckList(@RequestBody QueryBarLuckReq req) {
		Map<String,Object> queryParam = new HashMap<>();
		queryParam.put("startNum",req.getStartNum());
		queryParam.put("pageSize",req.getPageSize());
		queryParam.put("title",req.getTitle());
		List<Map<String,Object>> dataList = barLuckService.pageLuck(queryParam);
		int total = barLuckService.getLuckCount(queryParam);
		BaseResp resp = new BaseResp();
		resp.setDataList(dataList);
		resp.setTotalNum(total);
		resp.setRetMessage("分页查询活动信息成功");
		return resp;
	}

	/**
	 * 新增活动
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	@LogAnnotation(value ="新增活动",business = LogBusinessConstants.BAR_LUCK,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp addLuck(@RequestBody UpdateBarLuckReq req) {
		BarLuck barLuck = new BarLuck();
		barLuck.setSn(req.getSn());
		barLuck.setTitle(req.getTitle());
		barLuck.setType(req.getType());
		barLuck.setDelivery(req.getDelivery());
		barLuck.setTotalNum(req.getTotalNum());
		barLuck.setLeftNum(req.getLeftNum());
		barLuck.setPicture(req.getPicture());
		barLuck.setStatus(req.getStatus());
		barLuck.setJoinType(req.getJoinType());
		barLuck.setJoinThreshold(req.getJoinThreshold());
		barLuck.setDailyType(req.getDailyType());
		barLuck.setDailyMaxTime(req.getDailyMaxTime());
		barLuck.setTotalMaxTime(req.getTotalMaxTime());
		barLuck.setFloor(req.getFloor());
		barLuck.setFloorAward(req.getFloorAward());
		barLuck.setStartTime(req.getStartTime());
		barLuck.setEndTime(req.getEndTime());
		barLuck.setRemark(req.getRemark());
		barLuckService.addLuck(barLuck);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增活动成功");
		return resp;
	}

	/**
	 * 修改活动信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	@LogAnnotation(value ="修改活动信息",business = LogBusinessConstants.BAR_LUCK,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateLuck(@RequestBody UpdateBarLuckReq req) {
		BarLuck barLuck = new BarLuck();
		barLuck.setId(req.getId());
		barLuck.setSn(req.getSn());
		barLuck.setTitle(req.getTitle());
		barLuck.setType(req.getType());
		barLuck.setDelivery(req.getDelivery());
		barLuck.setTotalNum(req.getTotalNum());
		barLuck.setLeftNum(req.getLeftNum());
		barLuck.setPicture(req.getPicture());
		barLuck.setStatus(req.getStatus());
		barLuck.setJoinType(req.getJoinType());
		barLuck.setJoinThreshold(req.getJoinThreshold());
		barLuck.setDailyType(req.getDailyType());
		barLuck.setDailyMaxTime(req.getDailyMaxTime());
		barLuck.setTotalMaxTime(req.getTotalMaxTime());
		barLuck.setFloor(req.getFloor());
		barLuck.setFloorAward(req.getFloorAward());
		barLuck.setStartTime(req.getStartTime());
		barLuck.setEndTime(req.getEndTime());
		barLuck.setRemark(req.getRemark());
		barLuckService.updateLuck(barLuck);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改活动信息成功");
		return resp;
	}

	/**
	 * 删除活动信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除活动信息",business = LogBusinessConstants.BAR_LUCK,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteLuck(@RequestBody BaseDeleteReq req) {
		barLuckService.deleteLuck(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除活动信息成功");
		return resp;
	}

	/**
	 * 修改活动状态
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("status/update")
	@LogAnnotation(value ="修改活动状态",business = LogBusinessConstants.BAR_LUCK,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateLuckStatus(@RequestBody BaseDeleteReq req) {
		barLuckService.updateLuckStatus(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改活动状态成功");
		return resp;
	}

	/**
	 * 修改活动信息
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("copy")
	@LogAnnotation(value ="复制活动信息",business = LogBusinessConstants.BAR_LUCK,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp copyLuck(@RequestBody UpdateBarLuckReq req) {
		BarLuck barLuck = new BarLuck();
		barLuck.setId(req.getId());
		barLuck.setSn(req.getSn());
		barLuck.setTitle(req.getTitle());
		barLuck.setType(req.getType());
		barLuck.setDelivery(req.getDelivery());
		barLuck.setTotalNum(req.getTotalNum());
		barLuck.setLeftNum(req.getLeftNum());
		barLuck.setPicture(req.getPicture());
		barLuck.setStatus(req.getStatus());
		barLuck.setJoinType(req.getJoinType());
		barLuck.setJoinThreshold(req.getJoinThreshold());
		barLuck.setDailyType(req.getDailyType());
		barLuck.setDailyMaxTime(req.getDailyMaxTime());
		barLuck.setTotalMaxTime(req.getTotalMaxTime());
		barLuck.setFloor(req.getFloor());
		barLuck.setFloorAward(req.getFloorAward());
		barLuck.setStartTime(req.getStartTime());
		barLuck.setEndTime(req.getEndTime());
		barLuck.setRemark(req.getRemark());
		barLuckService.copyLuck(barLuck,req.getSubInfo());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改活动信息成功");
		return resp;
	}

	/**
	 * 导出论坛活动信息
	 *
	 * @return 导出数据
	 */
	@PostMapping("export")
	@LogAnnotation(value = "导出论坛活动信息", business = LogBusinessConstants.BAR_LUCK, operateType = LogOperateConstants.OPERATE_EXPORT)
	public void exportLuckInfo(HttpServletResponse response) {
		barLuckService.exportLuckInfo(response);
	}

	/**
	 * 分页查询活动参与记录信息
	 *
	 * @param req 请求信息
	 * @return 活动配置信息列表
	 */
	@PostMapping("flow/page")
	public BaseResp pageLuckFlowList(@RequestBody QueryBaseReq req) {
		Map<String,Object> queryParam = new HashMap<>();
		queryParam.put("startNum",req.getStartNum());
		queryParam.put("pageSize",req.getPageSize());
		queryParam.put("luckId",req.getId());
		List<Map<String,Object>> dataList = barLuckService.pageLuckFlow(queryParam);
		int total = barLuckService.getLuckFlowCount(queryParam);
		BaseResp resp = new BaseResp();
		resp.setDataList(dataList);
		resp.setTotalNum(total);
		resp.setRetMessage("分页查询活动参与记录信息成功");
		return resp;
	}
}
